package com.bootdo.program.weixin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.weixin.domain.WxAdvertDO;
import com.bootdo.program.weixin.service.WxAdvertService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 微信广告表
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-03 21:20:52
 */
 
@Controller
@RequestMapping("/wallpaper/wxAdvert")
public class WxAdvertController extends BaseController {
	@Autowired
	private WxAdvertService wxAdvertService;

	@GetMapping()
	@RequiresPermissions("wallpaper:wxAdvert:wxAdvert")
	String WxAdvert(){
	    return "wallpaper/wxAdvert/wxAdvert";
	}

	@GetMapping("/get")
	@ResponseBody
	R get (){
		String appid = isAdmin() ? "wxe763c88aa16d0565" : (null == defaultAppid() ? "noData" : defaultAppid());
		WxAdvertDO wxAdvert = wxAdvertService.getByAppid(appid);
		Map<String,Object> map = new HashMap<>();
		map.put("wxAdvert",wxAdvert);
		return R.ok(map);
	}
	

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( WxAdvertDO wxAdvert){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(null == defaultAppid()){
			return R.error("保存失败，请先配置小程序appid");
		}
		wxAdvert.setAppid(defaultAppid());
		if(null == wxAdvert.getId()){
			wxAdvertService.save(wxAdvert);
			return R.ok();
		}
		wxAdvertService.update(wxAdvert);
		return R.ok();
	}
	

}
